
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>内置基于类的视图 API &#8212; Django 3.2.6.dev 文档</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../genindex.html" />
    <link rel="search" title="搜索" href="../../search.html" />
    <link rel="next" title="基础视图" href="base.html" />
    <link rel="prev" title="系统检查框架" href="../checks.html" />



 
<script src="../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 3.2.6.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="../checks.html" title="系统检查框架">previous</a>
     |
    <a href="../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="base.html" title="基础视图">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-class-based-views-index">
            
  <div class="section" id="s-built-in-class-based-views-api">
<span id="built-in-class-based-views-api"></span><h1>内置基于类的视图 API<a class="headerlink" href="#built-in-class-based-views-api" title="永久链接至标题">¶</a></h1>
<p>基于类的视图 API 参考。关于介绍性材料，请参见 <a class="reference internal" href="../../topics/class-based-views/index.html"><span class="doc">Class-based views</span></a> 主题指南。</p>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="base.html">基础视图</a><ul>
<li class="toctree-l2"><a class="reference internal" href="base.html#view"><code class="docutils literal notranslate"><span class="pre">View</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="base.html#templateview"><code class="docutils literal notranslate"><span class="pre">TemplateView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="base.html#redirectview"><code class="docutils literal notranslate"><span class="pre">RedirectView</span></code></a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="generic-display.html">通用显示视图</a><ul>
<li class="toctree-l2"><a class="reference internal" href="generic-display.html#detailview"><code class="docutils literal notranslate"><span class="pre">DetailView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="generic-display.html#listview"><code class="docutils literal notranslate"><span class="pre">ListView</span></code></a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="generic-editing.html">通用编辑视图</a><ul>
<li class="toctree-l2"><a class="reference internal" href="generic-editing.html#formview"><code class="docutils literal notranslate"><span class="pre">FormView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="generic-editing.html#createview"><code class="docutils literal notranslate"><span class="pre">CreateView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="generic-editing.html#updateview"><code class="docutils literal notranslate"><span class="pre">UpdateView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="generic-editing.html#deleteview"><code class="docutils literal notranslate"><span class="pre">DeleteView</span></code></a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="generic-date-based.html">通用日期视图</a><ul>
<li class="toctree-l2"><a class="reference internal" href="generic-date-based.html#archiveindexview"><code class="docutils literal notranslate"><span class="pre">ArchiveIndexView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="generic-date-based.html#yeararchiveview"><code class="docutils literal notranslate"><span class="pre">YearArchiveView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="generic-date-based.html#montharchiveview"><code class="docutils literal notranslate"><span class="pre">MonthArchiveView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="generic-date-based.html#weekarchiveview"><code class="docutils literal notranslate"><span class="pre">WeekArchiveView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="generic-date-based.html#dayarchiveview"><code class="docutils literal notranslate"><span class="pre">DayArchiveView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="generic-date-based.html#todayarchiveview"><code class="docutils literal notranslate"><span class="pre">TodayArchiveView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="generic-date-based.html#datedetailview"><code class="docutils literal notranslate"><span class="pre">DateDetailView</span></code></a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="mixins.html">基于类的视图混入</a><ul>
<li class="toctree-l2"><a class="reference internal" href="mixins-simple.html">简单混入</a><ul>
<li class="toctree-l3"><a class="reference internal" href="mixins-simple.html#contextmixin"><code class="docutils literal notranslate"><span class="pre">ContextMixin</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-simple.html#templateresponsemixin"><code class="docutils literal notranslate"><span class="pre">TemplateResponseMixin</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="mixins-single-object.html">单对象混入</a><ul>
<li class="toctree-l3"><a class="reference internal" href="mixins-single-object.html#singleobjectmixin"><code class="docutils literal notranslate"><span class="pre">SingleObjectMixin</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-single-object.html#singleobjecttemplateresponsemixin"><code class="docutils literal notranslate"><span class="pre">SingleObjectTemplateResponseMixin</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="mixins-multiple-object.html">多对象混入</a><ul>
<li class="toctree-l3"><a class="reference internal" href="mixins-multiple-object.html#multipleobjectmixin"><code class="docutils literal notranslate"><span class="pre">MultipleObjectMixin</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-multiple-object.html#multipleobjecttemplateresponsemixin"><code class="docutils literal notranslate"><span class="pre">MultipleObjectTemplateResponseMixin</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="mixins-editing.html">编辑混入</a><ul>
<li class="toctree-l3"><a class="reference internal" href="mixins-editing.html#formmixin"><code class="docutils literal notranslate"><span class="pre">FormMixin</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-editing.html#modelformmixin"><code class="docutils literal notranslate"><span class="pre">ModelFormMixin</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-editing.html#processformview"><code class="docutils literal notranslate"><span class="pre">ProcessFormView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-editing.html#deletionmixin"><code class="docutils literal notranslate"><span class="pre">DeletionMixin</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="mixins-date-based.html">基于日期的混入</a><ul>
<li class="toctree-l3"><a class="reference internal" href="mixins-date-based.html#yearmixin"><code class="docutils literal notranslate"><span class="pre">YearMixin</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-date-based.html#monthmixin"><code class="docutils literal notranslate"><span class="pre">MonthMixin</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-date-based.html#daymixin"><code class="docutils literal notranslate"><span class="pre">DayMixin</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-date-based.html#weekmixin"><code class="docutils literal notranslate"><span class="pre">WeekMixin</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-date-based.html#datemixin"><code class="docutils literal notranslate"><span class="pre">DateMixin</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-date-based.html#basedatelistview"><code class="docutils literal notranslate"><span class="pre">BaseDateListView</span></code></a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="flattened-index.html">基于类的通用视图——扁平化索引</a><ul>
<li class="toctree-l2"><a class="reference internal" href="flattened-index.html#simple-generic-views">简单的通用视图</a><ul>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#view"><code class="docutils literal notranslate"><span class="pre">View</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#templateview"><code class="docutils literal notranslate"><span class="pre">TemplateView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#redirectview"><code class="docutils literal notranslate"><span class="pre">RedirectView</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="flattened-index.html#detail-views">详情视图</a><ul>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#detailview"><code class="docutils literal notranslate"><span class="pre">DetailView</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="flattened-index.html#list-views">列表视图</a><ul>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#listview"><code class="docutils literal notranslate"><span class="pre">ListView</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="flattened-index.html#editing-views">编辑视图</a><ul>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#formview"><code class="docutils literal notranslate"><span class="pre">FormView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#createview"><code class="docutils literal notranslate"><span class="pre">CreateView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#updateview"><code class="docutils literal notranslate"><span class="pre">UpdateView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#deleteview"><code class="docutils literal notranslate"><span class="pre">DeleteView</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="flattened-index.html#date-based-views">基于日期的视图</a><ul>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#archiveindexview"><code class="docutils literal notranslate"><span class="pre">ArchiveIndexView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#yeararchiveview"><code class="docutils literal notranslate"><span class="pre">YearArchiveView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#montharchiveview"><code class="docutils literal notranslate"><span class="pre">MonthArchiveView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#weekarchiveview"><code class="docutils literal notranslate"><span class="pre">WeekArchiveView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#dayarchiveview"><code class="docutils literal notranslate"><span class="pre">DayArchiveView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#todayarchiveview"><code class="docutils literal notranslate"><span class="pre">TodayArchiveView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#datedetailview"><code class="docutils literal notranslate"><span class="pre">DateDetailView</span></code></a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="s-specification">
<span id="specification"></span><h2>规范<a class="headerlink" href="#specification" title="永久链接至标题">¶</a></h2>
<p>基于类的视图所服务的每个请求都有一个独立的状态，因此，在实例上存储状态变量是安全的（即，<code class="docutils literal notranslate"><span class="pre">self.foo</span> <span class="pre">=</span> <span class="pre">3</span></code> 是一个线程安全的操作）。</p>
<p>基于类的视图使用 <code class="xref py py-meth docutils literal notranslate"><span class="pre">as_view()</span></code> 类方法部署到一个 URL 模式中。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;view/&#39;</span><span class="p">,</span> <span class="n">MyView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="n">size</span><span class="o">=</span><span class="mi">42</span><span class="p">)),</span>
<span class="p">]</span>
</pre></div>
</div>
<div class="admonition-thread-safety-with-view-arguments admonition">
<p class="first admonition-title">线程安全与视图参数</p>
<p class="last">传递给视图的参数在视图的每个实例之间共享。这意味着你不应该使用列表、字典或任何其他可更改的对象作为视图的参数。如果你这样做了，而共享对象又被修改了，那么访问你视图的一个用户的行为可能会对访问同一视图的后续用户产生影响。</p>
</div>
<p>传入 <code class="xref py py-meth docutils literal notranslate"><span class="pre">as_view()</span></code> 的参数将被分配到用于服务请求的实例上。使用前面的例子，这意味着在 <code class="docutils literal notranslate"><span class="pre">MyView</span></code> 上的每个请求都能够使用 <code class="docutils literal notranslate"><span class="pre">self.size</span></code>。参数必须与类上已经存在的属性相对应（在 <code class="docutils literal notranslate"><span class="pre">hasattr</span></code> 检查时返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>）。</p>
</div>
<div class="section" id="s-base-vs-generic-views">
<span id="base-vs-generic-views"></span><h2>基础 vs 通用视图<a class="headerlink" href="#base-vs-generic-views" title="永久链接至标题">¶</a></h2>
<p>基于类的基本视图可被视为 <em>父</em> 类视图，可自行使用或继承。它们可能无法提供项目所需的所有功能，在这种情况下，有混入来扩展基本视图的功能。</p>
<p>Django 的通用视图是在这些基础视图的基础上建立起来的，是作为显示对象详情等常见使用模式的快捷方式而开发的。它们采用了视图开发中的某些常见的习惯和模式，并将其抽象化，这样你就可以快速编写数据的通用视图，而不必重复。</p>
<p>大多数通用视图需要使用 <code class="docutils literal notranslate"><span class="pre">queryset</span></code> 键，它是一个 <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> 实例；关于 <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> 对象的更多信息，请参见 <a class="reference internal" href="../../topics/db/queries.html"><span class="doc">Making queries</span></a>。</p>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">内置基于类的视图 API</a><ul>
<li><a class="reference internal" href="#specification">规范</a></li>
<li><a class="reference internal" href="#base-vs-generic-views">基础 vs 通用视图</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="../checks.html"
                        title="上一章">系统检查框架</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="base.html"
                        title="下一章">基础视图</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/ref/class-based-views/index.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">7月 23, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="../checks.html" title="系统检查框架">previous</a>
     |
    <a href="../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="base.html" title="基础视图">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>